import { DEFAULT_PRIMARY, piniaPersistConfig } from "@/config";
import type { GlobalStoreConfig } from "@/typings";
import { defineStore } from "pinia";
import { reactive, ref } from "vue";

const key: string = "useGlobalStore";

export const useGlobalStore = defineStore(
  key,
  () => {
    const globalConfig: GlobalStoreConfig = reactive({
      lang: null,
      maximize: false,
      isDark: false,
      isCollapse: true,
      breadcrumb: true,
      breadcrumbIcon: true,
      tabs: true,
      tabsIcon: true,
      footer: false,
      openDrawer: false,
      isPanorama: false,
      assemblySize: "default",
      menuPrimary: "",
      sysPrimary: DEFAULT_PRIMARY
    });

    const date = ref<string>("");

    // 清除全局配置
    const clearGlobalConfig = () => {
      Object.assign(globalConfig, {
        lang: null,
        maximize: false,
        isDark: false,
        isCollapse: true,
        breadcrumb: true,
        breadcrumbIcon: true,
        tabs: true,
        tabsIcon: true,
        footer: false,
        openDrawer: false,
        isPanorama: false,
        assemblySize: "default",
        menuPrimary: "",
        sysPrimary: DEFAULT_PRIMARY
      });
    };

    return {
      globalConfig,
      date,
      clearGlobalConfig
    };
  },
  {
    persist: piniaPersistConfig(key, localStorage, ["globalConfig"])
  }
);
